package org.skydiveapplibrary.ui;

import org.skydiveapplibrary.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class FreeFallTimePreference extends DialogPreference
{
	private TextView ffTimeHoursField;
	private TextView ffTimeMinutesField;
	private TextView ffTimeSecondsField;
	private Integer initialValue;

	public FreeFallTimePreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		setDialogLayoutResource(R.layout.freefall_time_preference);
	}

	// Update the text with saved values
	@Override
    protected void onBindDialogView(View view)
	{
        super.onBindDialogView(view);
        
        if (initialValue != null)
        {
    		// init fields
			ffTimeHoursField = (TextView) view.findViewById(R.id.FreefallTimeHoursField);
			ffTimeMinutesField = (TextView) view.findViewById(R.id.FreefallTimeMinutesField);
			ffTimeSecondsField = (TextView) view.findViewById(R.id.FreefallTimeSecondsField);
			int hours = initialValue / 3600;
			int secondsLeftFromHours = initialValue % 3600;
			int minutes = secondsLeftFromHours / 60;
			int seconds = secondsLeftFromHours % 60;
			ffTimeHoursField.setText(FormatUtility.toNumericString(hours));
			ffTimeMinutesField.setText(FormatUtility.toNumericString(minutes));
			ffTimeSecondsField.setText(FormatUtility.toNumericString(seconds));
        }
	}
	
	@Override
    protected void onDialogClosed(boolean positiveResult)
	{
        super.onDialogClosed(positiveResult);
        
        if (positiveResult)
        {
        	// get new freefall time
    		int freefallTimeInSeconds = 0;

    		// get hours
    		String hoursString = ffTimeHoursField.getText() == null ? null : ffTimeHoursField.getText().toString();
    		freefallTimeInSeconds += FormatUtility.toInteger(hoursString, 0) * 60 * 60;
    		// get minutes
    		String minutesString = ffTimeMinutesField.getText() == null ? null : ffTimeMinutesField.getText().toString();
    		freefallTimeInSeconds += FormatUtility.toInteger(minutesString, 0) * 60;
    		// get seconds
    		String secondsString = ffTimeSecondsField.getText() == null ? null : ffTimeSecondsField.getText().toString();
    		freefallTimeInSeconds += FormatUtility.toInteger(secondsString, 0);

    		// save new value
    		persistInt(freefallTimeInSeconds);
        }
    }
	
	@Override
	protected void onSetInitialValue(boolean restorePersistedValue,
			Object defaultValue) {
		int def = ( defaultValue instanceof Number ) ? (Integer)defaultValue
				: ( defaultValue != null ) ? Integer.parseInt(defaultValue.toString()) : 1;
		if ( restorePersistedValue ) {
			this.initialValue = getPersistedInt(def);
		}
		else this.initialValue = (Integer)defaultValue;
		
		persistInt(initialValue);
	}
		
	@Override
	protected Object onGetDefaultValue(TypedArray a, int index) {
		return a.getInt(index, 0);
	}
}
